package com.adams.dcxt.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adams.dcxt.bean.Dishes;
import com.adams.dcxt.bean.DishesType;
import com.adams.dcxt.core.Config;
import com.adams.dcxt.core.Page;
import com.adams.dcxt.service.DishesService;
import com.adams.dcxt.service.DishesTypeService;

public class DishesAction extends BaseAction {

    private static final long serialVersionUID = -2631561512464121713L;
    private DishesTypeService dishesTypeSerice;
    private DishesService dishesService;
    private String typeId; // 菜单类型ID
    private static Logger logger = LoggerFactory.getLogger(DishesAction.class);
    private String name; // 菜名
    private String ingr; // 原料
    private String imgHost = Config.getInstance().getString("img.host");

    // 按菜单查询
    public String queryDishesName() {
        Map response = new HashMap();
        Page<Dishes> page = new Page<Dishes>();
        setPage(page);
        try {
            page = dishesService.queryByDishesName(page, name);
            List<Dishes> list = page.getResultList();
            if (list != null) {
                setImgHost(list);
            }
            response.put("data", list);
            response.put("success", true);
            response.put("totalPage", page.getTotalPage());
        } catch (Exception e) {
            response.put("data", "");
            response.put("success", false);
            write(JSONObject.fromObject(response).toString());
            logger.error("按菜名查询菜单出错", e);
            return null;
        }
        write(JSONObject.fromObject(response).toString());
        return null;
    }

    /**
     * 查询原料
     * 
     * @return
     */
    public String queryIngr() {
        Map response = new HashMap();
        Page<Dishes> page = new Page<Dishes>();
        setPage(page);
        try {
            page = dishesService.queryByDishesIngredients(page, ingr);
            List<Dishes> list = page.getResultList();
            if (list != null) {
                setImgHost(list);
            }
            response.put("data", list);
            response.put("success", true);
            response.put("totalPage", page.getTotalPage());
        } catch (Exception e) {
            response.put("data", "");
            response.put("success", false);
            write(JSONObject.fromObject(response).toString());
            logger.error("根据原料查询出错", e);
            return null;
        }
        write(JSONObject.fromObject(response).toString());
        return null;
    }

    // 查询特价
    public String querySpecialRate() {
        Map response = new HashMap();
        Page<Dishes> page = new Page<Dishes>();
        setPage(page);
        try {
            page = dishesService.querySpecialRate(page);
            List<Dishes> list = page.getResultList();
            if (list != null) {
                setImgHost(list);
            }
            response.put("data", list);
            response.put("success", true);
            response.put("totalPage", page.getTotalPage());
        } catch (Exception e) {
            response.put("data", "");
            response.put("success", false);
            write(JSONObject.fromObject(response).toString());
            logger.error("获取特价菜单出错", e);
            return null;
        }
        write(JSONObject.fromObject(response).toString());
        return null;
    }

    // 查询口碑
    public String queryTop() {
        Map response = new HashMap();
        Page<Dishes> page = new Page<Dishes>();
        setPage(page);
        try {
            page = dishesService.queryTop(page);
            List<Dishes> list = page.getResultList();
            if (list != null) {
                setImgHost(list);
            }
            response.put("data", list);
            response.put("success", true);
            response.put("totalPage", page.getTotalPage());
        } catch (Exception e) {
            response.put("data", "");
            response.put("success", false);
            write(JSONObject.fromObject(response).toString());
            logger.error("获取口碑菜单出错", e);
            return null;
        }
        write(JSONObject.fromObject(response).toString());
        return null;
    }

    /**
     * 查询推荐菜
     * 
     * @return
     */
    public String queryRecommend() {

        Map response = new HashMap();
        Page<Dishes> page = new Page<Dishes>();
        setPage(page);
        try {
            page = dishesService.queryRecommend(page);
            List<Dishes> list = page.getResultList();
            if (list != null) {
                setImgHost(list);
            }
            response.put("data", list);
            response.put("success", true);
            response.put("totalPage", page.getTotalPage());
        } catch (Exception e) {
            response.put("data", "");
            response.put("success", false);
            write(JSONObject.fromObject(response).toString());
            logger.error("获取推荐菜单出错", e);
            return null;
        }

        write(JSONObject.fromObject(response).toString());

        return null;
    }

    /**
     * 查询所有菜类型
     * 
     * @return
     */
    public String getMenu() {
        Map response = new HashMap();
        try {
            List<DishesType> list = dishesTypeSerice.queryAll();
            response.put("data", list);
            response.put("success", true);
        } catch (Exception e) {
            response.put("data", "");
            response.put("success", false);
            logger.error("查询菜单类型错误：", e);
            return null;
        }
        write(JSONObject.fromObject(response).toString());
        return null;
    }

    /**
     * 根据类型ID分页查询菜单
     * 
     * @return
     */
    public String getDishesByTypeId() {

        Map response = new HashMap();
        Page<Dishes> page = new Page<Dishes>();
        setPage(page);
        if (StringUtils.isBlank(typeId)) {
            // 直接显示错误
            response.put("data", "");
            response.put("success", false);
            write(JSONObject.fromObject(response).toString());
            return null;
        }
        try {
            page = dishesService.queryByType(page, Long.parseLong(typeId));
        } catch (Exception e) {
            logger.error("查询错误:", e);
            response.put("data", "");
            response.put("success", false);
            write(JSONObject.fromObject(response).toString());
            return null;
        }
        Integer totalPage = page.getTotalPage();
        List<Dishes> list = page.getResultList();
        if (list != null) {
            setImgHost(list);
        }
        response.put("data", list);
        response.put("success", true);
        response.put("totalPage", totalPage);
        write(JSONObject.fromObject(response).toString());
        return null;
    }

    public void setImgHost(List<Dishes> list) {
        for (Dishes dishes : list) {
            dishes.setBigPic(imgHost + dishes.getBigPic());
        }
    }

   

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIngr() {
        return ingr;
    }

    public void setIngr(String ingr) {
        this.ingr = ingr;
    }


    public DishesTypeService getDishesTypeSerice() {
        return dishesTypeSerice;
    }

    public void setDishesTypeSerice(DishesTypeService dishesTypeSerice) {
        this.dishesTypeSerice = dishesTypeSerice;
    }

    public DishesService getDishesService() {
        return dishesService;
    }

    public void setDishesService(DishesService dishesService) {
        this.dishesService = dishesService;
    }

    public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }

 

}
